package com.ycuwq.datepicker.util;

import ohos.agp.colors.RgbColor;
import ohos.agp.utils.Color;

/**
 * 颜色线性渐变工具
 * Created by ycuwq on 2018/1/6.
 */
public class LinearGradient {
    /**
     * The constant M start color
     */
    private int mStartColor;
    /**
     * The constant M end color
     */
    private int mEndColor;
    /**
     * The constant M red start
     */
    private int mRedStart;
    /**
     * The constant M blue start
     */
    private int mBlueStart;
    /**
     * The constant M green start
     */
    private int mGreenStart;
    /**
     * The constant M red end
     */
    private int mRedEnd;
    /**
     * The constant M blue end
     */
    private int mBlueEnd;
    /**
     * The constant M green end
     */
    private int mGreenEnd;

    /**
     * Linear gradient
     *
     * @param startColor start color
     * @param endColor   end color
     */
    public LinearGradient(int startColor, int endColor) {
        mStartColor = startColor;
        mEndColor = endColor;
        updateColor();
    }


    /**
     * Set start color *
     *
     * @param startColor start color
     */
    public void setStartColor(int startColor) {
        mStartColor = startColor;
        updateColor();
    }

    /**
     * Set end color *
     *
     * @param endColor end color
     */
    public void setEndColor(int endColor) {
        mEndColor = endColor;
        updateColor();
    }

    /**
     * Update color
     */
    private void updateColor() {
        RgbColor startRgbColor = RgbColor.fromArgbInt(mStartColor);
        RgbColor endRgbColor = RgbColor.fromArgbInt(mEndColor);
        mRedStart = startRgbColor.getRed();
        mBlueStart = startRgbColor.getBlue();
        mGreenStart = startRgbColor.getGreen();
        mRedEnd = endRgbColor.getRed();
        mBlueEnd = endRgbColor.getBlue();
        mGreenEnd = endRgbColor.getGreen();
    }

    /**
     * Get color color
     *
     * @param ratio ratio
     * @return the color
     */
    public Color getColor(float ratio) {
        int red = (int) (mRedStart + ((mRedEnd - mRedStart) * ratio + 0.5));
        int greed = (int) (mGreenStart + ((mGreenEnd - mGreenStart) * ratio + 0.5));
        int blue = (int) (mBlueStart + ((mBlueEnd - mBlueStart) * ratio + 0.5));
        return new Color(Color.rgb(red, greed, blue));
    }
}
